﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ZK.MyControl
{
    public partial class UCMonthPicker : UserControl
    {
        public UCMonthPicker()
        {
            InitializeComponent();
            Init();
        }

        Dictionary<int, Button> dictionaryButton = new Dictionary<int, Button>();
        public EventHandler CompletedClick;    
      
        private int _year;
        public int Year
        {
            get {
                return _year;
            }
            set {
                _year = value;
                GetMonthCalendar();
            }
        
        }

        private int _month ;
        public int Month
        {
            get
            {
                return _month;
            }
            set
            {
                _month = value;
                CurrentButton = dictionaryButton[Month];
            }
        }

        private Button _currentButton;
        public Button CurrentButton {

            get { return _currentButton; }
            set {
                if (_currentButton != value)
                {
                    if (_currentButton != null)
                    {
                        _currentButton.BackColor = Color.Transparent;
                        _currentButton.ForeColor = SystemColors.ControlText;
                    }
                }
                _currentButton = value;
                _currentButton.BackColor = SystemColors.Highlight;
                _currentButton.ForeColor = Color.White;
            }
        }



        private void UCDateTimePicker_Load(object sender, EventArgs e)
        {
        }

        private void GetMonthCalendar()
        {
            lblSelectedYear.Text= Year.ToString();
        }


        private void Init()
        {

            dictionaryButton.Add(1, btn01);
            dictionaryButton.Add(2, btn02);
            dictionaryButton.Add(3, btn03);
            dictionaryButton.Add(4, btn04);
            dictionaryButton.Add(5, btn05);
            dictionaryButton.Add(6, btn06);
            dictionaryButton.Add(7, btn07);
            dictionaryButton.Add(8, btn08);
            dictionaryButton.Add(9, btn09);
            dictionaryButton.Add(10, btn10);
            dictionaryButton.Add(11, btn11);
            dictionaryButton.Add(12, btn12);

            Year = DateTime.Now.Year;
            Month = DateTime.Now.Month;
            lblToday.Text = "Month:"+ DateTime.Now.ToString("yyyy-MM");
        }

        private void panelLeft_Click(object sender, EventArgs e)
        {
               Year--;
        }

        private void panelRight_Click(object sender, EventArgs e)
        {
      
                Year++;
           
        }

        private void btn_Click(object sender, EventArgs e)
        {
                

             Button btn = (Button)sender;
             Month = int.Parse(btn.Tag.ToString());

             DateTime dateTime = new DateTime(Year,Month,1);
            if (CompletedClick != null)
            {
                CompletedClick(dateTime, null);
            }
        }

        private void lblToday_Click(object sender, EventArgs e)
        {
            Year = DateTime.Now.Year;
            Month = DateTime.Now.Month;
            DateTime dateTime = new DateTime(Year, Month, 1);
            if (CompletedClick != null)
            {
                CompletedClick(dateTime, null);
            }
        }
    }
}
